package com.geovis.emergency.spd.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.auth.util.AuthUtil;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillDutyUserService;
import com.geovis.emergency.spd.biz.pc.service.IPcMessageService;
import com.geovis.emergency.spd.biz.pc.service.IPcRollCallService;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDutyUser;
import com.geovis.emergency.spd.entity.pc.entity.PcMessage;
import com.geovis.emergency.spd.entity.pc.entity.PcRollCall;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcMessageConstant;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/6/24
 */
@Component
@AllArgsConstructor
public class MsgUtil {
    private static IPcMessageService messageService;

    private static IPcRollCallService pcRollCallService;

    private static IPcDrillDutyUserService pcDrillDutyUserService;
    private final IPcMessageService _messageService;
    private final IPcDrillDutyUserService _pcDrillDutyUserService;

    private final IPcRollCallService _pcRollCallService;


    @PostConstruct
    public void init(){
        messageService =  _messageService;
        pcDrillDutyUserService =  _pcDrillDutyUserService;
        pcRollCallService =  _pcRollCallService;
    }
    public void pushDrillAddMsg(String drill){

    }
    public void pushDrillUpdateMsg(String drill){

    }
    public void pushDrillStartMsg(String drill){

    }
    public static Set<String> getDutyUserIdList(String drillId){

        LambdaQueryWrapper<PcDrillDutyUser> wrapper = Wrappers.lambdaQuery(PcDrillDutyUser.class)
                .eq(PcDrillDutyUser::getPcDrillId, drillId)
                .isNotNull(PcDrillDutyUser::getPrincipalIds)
                .select(PcDrillDutyUser::getPrincipalIds);
        Set<String> idList = CollUtil.set(false);
        List<PcDrillDutyUser> list = pcDrillDutyUserService.list(wrapper);
        for (PcDrillDutyUser pcDrillDutyUser : list) {
            idList.addAll(StrUtil.split(pcDrillDutyUser.getPrincipalIds(),","));
        }
        return idList;
    }

    /**
     * 发送演练相关的各类消息
     * @param drillId 演练id
     * @param excludeUserId 排除的用户id，一般为当前操作人
     * @param type 类型
     * @param title 标题
     * @param content 内容
     * @param businessId 业务id
     */
    public static void sendDrillMsg(String drillId,String excludeUserId,String type,String title,String content,String businessId){
        Set<String> dutyUserIdList = MsgUtil.getDutyUserIdList(drillId);
        for (String userId : dutyUserIdList) {
            if(excludeUserId!=null && userId.equals(excludeUserId))
            {
                continue;
            }
            MsgUtil.pushMsgByUser(type,title,content,drillId,businessId,userId);
        }

    }
    /**
     * 向其他用户发起签到邀请
     * @param pcDrillId
     * @param pcRollCallId
     * @param personId
     */
    public static void sendRollCallNotice(String pcDrillId,String pcRollCallId, String personId,Integer callRollTime) {
        Map<String,String> data = new HashMap<>(4);
        data.put("pcDrillId",pcDrillId);
        data.put("personId",personId);
        data.put("pcRollCallId",pcRollCallId);
        data.put("callRollTime",String.valueOf(callRollTime));
        pushMsg(PcMessageConstant.TYPE_INVITE_ROLL_CALL_MSG,data,CollectionUtil.set(false,personId));
    }

    /**
     * 结束签到
     * @param pcDrillId
     */
    public static void stopRollCallNotice(String pcDrillId) {
        Map<String,String> data = new HashMap<>(1);
        data.put("pcDrillId",pcDrillId);
        Set<String> dutyUserIdList = getDutyUserIdList(pcDrillId);
        pushMsg(PcMessageConstant.TYPE_STOP_ROLL_CALL_MSG,data,dutyUserIdList);
    }
    /**
     * 用户点击点名确认
     * @param rollCallIdId
     * @param personId
     */
    public static void sendRollCallFeedback(String rollCallIdId, String personId) {
        PcRollCall pcRollCall = pcRollCallService.getById(rollCallIdId);
        if(ObjectUtil.isNull(pcRollCall)) {
            return;
        }
        Map<String,String> data = new HashMap<>(3);
        data.put("pcDrillId",pcRollCall.getPcDrillId());
        data.put("rollCallIdId",rollCallIdId);
        data.put("personId",personId);
        Set<String> dutyUserIdList = getDutyUserIdList(pcRollCall.getPcDrillId());
        if (StrUtil.isNotBlank(pcRollCall.getCreateUser())){
            dutyUserIdList.add(pcRollCall.getCreateUser());
        }
        pushMsg(PcMessageConstant.TYPE_ROLL_CALL_FEEDBACK_MSG,data,dutyUserIdList);
    }
    public static void startRollCall(String pcDrillId) {
        Map<String,String> data = new HashMap<>(1);
        data.put("pcDrillId",pcDrillId);
        Set<String> dutyUserIdList = getDutyUserIdList(pcDrillId);
        pushMsg(PcMessageConstant.TYPE_START_ROLL_CALL_MSG,data,dutyUserIdList);
    }
    /**
     * 指定用户发送消息
     * @param type
     * @param data
     * @param userList
     */
    public static void pushMsg(String type, Object data, Set<String> userList){
        messageService.sendSocketMessage(type,data,userList);
    }

    /**
     * 按全部用户发送消息
     * @param type
     * @param data
     */
    public static void pushAllMsg(String type, Object data){
        messageService.sendSocketMessage(type,data);
    }
    /**
     * 发送全部消息
     * @param type
     * @param title
     * @param content
     * @param drillId
     * @param businessId
     */
    public static void pushAllMsg(String type,String title,String content,String drillId,String businessId){
        PcMessage message = PcMessage.newInstance(type, title, content, drillId, businessId);
        messageService.save(message);
        // 发送socket消息提示
        messageService.sendSocketMessage(message);
    }

    public static void pushMsgByDrillUser(String type,String title,String content,String drillId,String businessId){
        Set<String> dutyUserIdList = MsgUtil.getDutyUserIdList(drillId);
        for (String userId : dutyUserIdList) {
            pushMsgByUser(type,title,content,drillId,businessId,userId);
        }
    }

    /**
     * 指定用户发送消息
     * @param type
     * @param title
     * @param content
     * @param drillId
     * @param businessId
     * @param userId
     */
    public static void pushMsgByUser(String type,String title,String content,String drillId,String businessId,String userId){
        PcMessage message = PcMessage.newInstance(type, title, content, drillId, businessId).setAcceptId(userId);
        messageService.save(message);
        // 发送socket消息提示
        messageService.sendSocketMessage(message, CollUtil.set(false,userId));
    }

    /**
     * 指定岗位发送消息
     * @param type
     * @param title
     * @param content
     * @param drillId
     * @param businessId
     * @param postId
     */
    public static void pushMsgByPost(String type,String title,String content,String drillId,String businessId,String postId){
        PcMessage message = PcMessage.newInstance(type, title, content, drillId, businessId);
        messageService.save(message);
        // 发送socket消息提示
        messageService.sendSocketMessage(message);
    }

    /**
     * 保存消息
     * @param type
     * @param title
     * @param content
     * @param drillId
     * @param businessId
     */
    public static void saveMsg(String type,String title,String content,String drillId,String businessId){
        PcMessage message = PcMessage.newInstance(type, title, content, drillId, businessId);
        messageService.save(message);
    }

    /**
     * 保存消息
     * @param type
     * @param title
     * @param content
     * @param drillId
     * @param businessId
     * @param userId
     */
    public static void saveMsg(String type,String title,String content,String drillId,String businessId,String userId){
        PcMessage message = PcMessage.newInstance(type, title, content, drillId, businessId).setAcceptId(userId);
        messageService.save(message);
    }
}
